package cn.wolfcode.p2p.website.web.controller;

import cn.wolfcode.p2p.base.anno.RequiredLogin;
import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.JSONResult;
import cn.wolfcode.p2p.base.util.LoginInfoContext;
import cn.wolfcode.p2p.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

// 实名认证
@Controller
public class RealAuthController {
    @Autowired
    private IRealAuthService realAuthService;
    @Autowired
    private IUserInfoService userInfoService;

    // 判断是否在
    @RequestMapping("/realAuth")
    @RequiredLogin
    public String toRealAuth(Model model){
        // 判断是否提交实名认证, 有则
        UserInfo userInfo = userInfoService.get(LoginInfoContext.getCurrentLoginInfo().getId());
        if (userInfo.getRealAuthId() != null){
            model.addAttribute("auditing", !userInfo.getHasRealAuth());
            model.addAttribute("realAuth", realAuthService.get(userInfo.getRealAuthId()));
            return "realAuth_result";
        }
        return "realAuth";
    }

    @RequestMapping("/realAuth_save")
    @ResponseBody
    public Object saveRealAuth(RealAuth vo){
        // 需要设置申请人id, 申请时间
        realAuthService.saveRealAuth(vo);
        return new JSONResult();
    }


    // 图片上传
    @RequestMapping("/uploadImage")
    @ResponseBody
    public String upload(MultipartFile pic){
        String upload = UploadUtil.upload(pic);
        return upload;
    }
}
